/**
 * @file common.js
 */

/*
 * @file config.js
 -- destoon path
 var DTPath = "http://127.0.0.1/ixdcw/";
 -- skin path
 var SKPath = "http://127.0.0.1/ixdcw/static/css/imgs/";
 -- extend path
 var EXPath = "http://127.0.0.1/ixdcw/extend/";
 -- cookie domain
 var CKDomain = "";
 -- cookie path
 var CKPath = "/";
 -- cookie prefix
 var CKPrex = "DPg_";
 */

/**
 * 是否IE浏览器
 * @type {Boolean}
 */
var isIE = (document.all && window.ActiveXObject && !window.opera) ? true : false;

/**
 * 是否gecko内核
 * @type {Boolean}
 */
var isGecko = navigator.userAgent.indexOf('WebKit') != -1;

/**
 * Domain Url
 * @type {String}
 */
var DMURL = document.location.protocol+'//'+location.hostname+(location.port ? ':'+location.port : '')+'/';

/**
 * ajax path, 这里是ajax调用的
 * @type {String}
 */
var AJPath = (DTPath.indexOf('://') == -1 ? DTPath : (DTPath.indexOf(DMURL) == -1 ? DMURL : DTPath)) + 'ajax.php';

if (isIE) try {
    // 用这个方法告诉IE6缓存背景图片, 解决IE下图片作为背景时，有鼠标操作时的抖动问题
    document.execCommand("BackgroundImageCache", false, true);
} catch (e) {}

/**
 * Ajax 请求对象
 */
var xmlHttp;

/**
 * 用来执行一系列函数, 然后返回可行的那一位
 * @type {Object}
 */
var Try = {
	these: function() {
		var returnedFun;
        for (var i = 0; i < arguments.length; i++) {
            var fun = arguments[i];
            try {
                // 这里执行错误的话会抛出一个异常 . 然后就继续执行下一个函数
                returnedFun = fun();
                break;
            } catch (e) {
                // empty
            }
        }
		return returnedFun;
	}
}

/**
 * ajax 请求
 * makeRequest('action=property&itemid=itemid&catid=catid', AJPath, 'into_property');
 * @param queryString   查询的ID
 * @param php           查询的页面
 * @param func          状态改变时候调用的函数
 * @param method        方法,默认是 post
 */
function makeRequest(queryString, php, func, method) {
	xmlHttp = Try.these(
        function () {
            // IE7+, Firefox, Chrome, Safari, and Opera
            return new XMLHttpRequest()
        },
        /*
        jquery 中 xhr的写法调用中摒弃了这个 1.4.2
        function () {
            return new ActiveXObject('Msxml2.XMLHTTP')
        },
        */
        function () {
            // IE5 and IE6
            return new ActiveXObject('Microsoft.XMLHTTP')
        }
	);
    method = (typeof method == 'undefined') ? 'post' : 'get';
    if (func) xmlHttp.onreadystatechange = eval(func);
    xmlHttp.open(method, method == 'post' ? php : php + '?' + queryString, true);
    xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlHttp.send(method == 'post' ? queryString : null);
}
/**
 * 等于 jQuery的 $(i)
 * @param i     id 值
 * @return {HTMLElement}
 * @constructor
 */
function Dd(i) {
    return document.getElementById(i);
}

/**
 * 显示 i 元素
 * @param i
 * @constructor
 */
function Ds(i) {
    Dd(i).style.display = '';
}

/**
 * 隐藏 i 元素
 * @param i
 * @constructor
 */
function Dh(i) {
    Dd(i).style.display = 'none';
}

/**
 * 聚焦 i 元素
 * @param i
 * @constructor
 */
function Df(i) {
    Dd(i).focus();
}

/**
 * Tab函数
 * @type {Number}
 */
var tID = 0;
function Tab(ID) {
    var tTab = Dd('Tab' + tID);
    var tTabs = Dd('Tabs' + tID);
    var Tab = Dd('Tab' + ID);
    var Tabs = Dd('Tabs' + ID);
    if (ID != tID) {
        tTab.className = 'tab';
        Tab.className = 'tab_on';
        tTabs.style.display = 'none';
        Tabs.style.display = '';
        tID = ID;
        try {
            Dd('tab').value = ID;
        } catch (e) {
        }
    }
}
function checkall(f, t) {
    var t = t ? t : 1;
    for (var i = 0; i < f.elements.length; i++) {
        var e = f.elements[i];
        if (e.type != 'checkbox') continue;
        if (t == 1) e.checked = e.checked ? false : true;
        if (t == 2) e.checked = true;
        if (t == 3) e.checked = false;
    }
}
function stoinp(s, i, p) {
    if (Dd(i).value) {
        var p = p ? p : ',';
        var a = Dd(i).value.split(p);
        for (var j = 0; j < a.length; j++) {
            if (s == a[j]) return;
        }
        Dd(i).value += p + s;
    } else {
        Dd(i).value = s;
    }
}
function select_op(i, v) {
    var o = Dd(i);
    for (var i = 0; i < o.options.length; i++) {
        if (o.options[i].value == v) {
            o.options[i].selected = true;
            break;
        }
    }
}
function Dmsg(str, i, s, t) {
    var t = t ? t : 5000;
    var s = s ? true : false;
    try {
        if (s) {
            window.scrollTo(0, 0);
        }
        Dd('d' + i).innerHTML = '<img src="' + SKPath + 'common/check_error.gif" width="12" height="12" align="absmiddle"/> ' + str + sound('tip');
        Dd(i).focus();
    } catch (e) {
    }
    window.setTimeout(function () {
        Dd('d' + i).innerHTML = '';
    }, t);
}
function Inner(i, s) {
    try {
        Dd(i).innerHTML = s;
    } catch (e) {
    }
}
function InnerTBD(i, s) {
    try {
        Dd(i).innerHTML = s;
    } catch (e) {
        Dd(i).parentNode.outerHTML = Dd(i).parentNode.outerHTML.replace(Dd(i).innerHTML, s);
    }
}
function confirmURI(m, f) {
    if (confirm(m)) Go(f);
}
function Go(u) {
    window.location = u;
}

/**
 * 显示提示信息
 * @link http://wiki.ixdcw.com:8088/index.php?doc-view-54
 * @param m  信息内容
 * @param t  时间
 */
function showmsg(m, t) {
    var t = t ? t : 1000;
    var s = m.indexOf(L['str_delete']) != -1 ? 'delete' : 'ok';
    try {
        Dd('msgbox').style.display = '';
        Dd('msgbox').innerHTML = m + sound(s);
        window.setTimeout('closemsg();', t);
    } catch (e) {
    }
}
function closemsg() {
    try {
        Dd('msgbox').innerHTML = '';
        Dd('msgbox').style.display = 'none';
    } catch (e) {
    }
}
function sound(f) {
    return '<div style="float:left;"><embed src="' + DTPath + 'file/flash/' + f + '.swf" quality="high" type="application/x-shockwave-flash" height="0" width="0" hidden="true"/></div>';
}
function Eh(t) {
    var t = t ? t : 'select';
    if (isIE) {
        var arVersion = navigator.appVersion.split("MSIE");
        var IEversion = parseFloat(arVersion[1]);
        if (IEversion >= 7 || IEversion < 5) return;
        var ss = document.body.getElementsByTagName(t);
        for (var i = 0; i < ss.length; i++) {
            ss[i].style.visibility = 'hidden';
        }
    }
}
function Es(t) {
    var t = t ? t : 'select';
    if (isIE) {
        var arVersion = navigator.appVersion.split("MSIE");
        var IEversion = parseFloat(arVersion[1]);
        if (IEversion >= 7 || IEversion < 5) return;
        var ss = document.body.getElementsByTagName(t);
        for (var i = 0; i < ss.length; i++) {
            ss[i].style.visibility = 'visible';
        }
    }
}
function FCKLen(i) {
    var i = i ? i : 'content';
    var o = FCKeditorAPI.GetInstance(i);
    var d = o.EditorDocument;
    var l;
    if (document.all) {
        l = d.body.innerText.length;
    } else {
        var r = d.createRange();
        r.selectNodeContents(d.body);
        l = r.toString().length;
    }
    return l;
}
function FCKXHTML(i) {
    var i = i ? i : 'content';
    return FCKeditorAPI.GetInstance(i).GetXHTML(true);
}
function Tb(d, t, p, c) {
    for (var i = 1; i <= t; i++) {
        if (d == i) {
            Dd(p + '_t_' + i).className = c + '_2';
            Ds(p + '_c_' + i);
        } else {
            Dd(p + '_t_' + i).className = c + '_1';
            Dh(p + '_c_' + i);
        }
    }
}
function is_captcha(v) {
    if (v == L['str_captcha']) return false;
    if (v.match(/^[a-z0-9A-Z]{1,}$/)) {
        return v.match(/^[a-z0-9A-Z]{4,}$/);
    } else {
        return v.length > 1;
    }
}
function ext(v) {
    return v.substring(v.lastIndexOf('.') + 1, v.length);
}
function set_cookie(n, v, d) {
    var e = '';
    var f = d ? d : 365;
    e = new Date((new Date()).getTime() + f * 86400000);
    e = "; expires=" + e.toGMTString();
    document.cookie = CKPrex + n + "=" + v + ((CKPath == "") ? "" : ("; path=" + CKPath)) + ((CKDomain == "") ? "" : ("; domain=" + CKDomain)) + e;
}
function get_cookie(n) {
    var v = '';
    var s = CKPrex + n + "=";
    if (document.cookie.length > 0) {
        o = document.cookie.indexOf(s);
        if (o != -1) {
            o += s.length;
            end = document.cookie.indexOf(";", o);
            if (end == -1) end = document.cookie.length;
            v = unescape(document.cookie.substring(o, end));
        }
    }
    return v;
}
function del_cookie(n) {
    var e = new Date((new Date()).getTime() - 1);
    e = "; expires=" + e.toGMTString();
    document.cookie = CKPrex + n + "=" + escape("") + ";path=/" + e;
}
function substr_count(str, exp) {
    if (str == '') return 0;
    var s = str.split(exp);
    return s.length - 1;
}
function lang(s, a) {
    for (var i = 0; i < a.length; i++) {
        s = s.replace('{V' + i + '}', a[i]);
    }
    return s;
}
document.onkeydown = function (e) {
    var k = typeof e == 'undefined' ? event.keyCode : e.keyCode;
    if (k == 37) {
        try {
            if (Dd('destoon_previous').value && typeof document.activeElement.name == 'undefined')Go(Dd('destoon_previous').value);
        } catch (e) {
        }
    } else if (k == 39) {
        try {
            if (Dd('destoon_next').value && typeof document.activeElement.name == 'undefined')Go(Dd('destoon_next').value);
        } catch (e) {
        }
    } else if (k == 38 || k == 40 || k == 13) {
        try {
            if (Dd('search_tips').style.display != 'none' || Dd('search_tips').innerHTML != '') {
                SCTip(k);
                return false;
            }
        } catch (e) {
        }
    } else if (k == 27) {
        try {
            if (Dd('Dtop') != null) cDialog();
        } catch (e) {
        }
    }
}
function showMenu() {
    document.getElementById("menu-cat").style.display = "block";
}
function hideMenu() {
    document.getElementById("menu-cat").style.display = "none";
}


function xdChangeAndLocation(param, value, tripFile) {
    var stringObj = window.location.href;
    typeof tripFile == 'undefined' ? tripFile = window.location.pathname : tripFile;
    var lstr = "&";

    if (stringObj.indexOf(tripFile+'?') == -1) {
        lstr = "?";
    }
    var reg = new RegExp(param + "=[0-9a-zA-Z,]*", "g"); //创建正则RegExp对象
    var urlGo = "";
    var ch = stringObj.indexOf(param+'=');
    if (ch == -1) {
        urlGo = urlGo + stringObj + lstr + param + "=" + value;
    }
    if (ch != -1) {
        urlGo = stringObj.replace(reg, param + "=" + value);
    }
    window.location = urlGo;
}

function go(p, v, f) {
	xdChangeAndLocation(p,v,f);
}
/**
 * 用于点击回到顶部
 * @Author  Mark
 */
function topit() {
	c = null;
	b = document.documentElement.scrollTop + document.body.scrollTop;
	b = Math.floor(b / 2);
	window.scrollTo(0, b);
	if (b > 2) {
		c = setTimeout("sidebarToTop()", 40)
	} else {
		window.scrollTo(0, 0);
		clearTimeout(c)
	}
}

function fragment_help_heartBeat(_sel){
	$_sel = jQuery(_sel);
	jQuery(window).scroll(function(){
		if ((document.documentElement.scrollTop + document.body.scrollTop) > 0) {
			$_sel.show();
		} else {
			$_sel.hide();
		}
	})
}

function setHomepage() {
	if ( document.all ) {
		document.body.style.behavior = 'url(#default#homepage)';
		document.body.setHomePage(window.location.href);
	} else if ( window.sidebar ) {
		if ( window.netscape ) {
			try {
				netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
			} catch (e) {
				alert("该操作被浏览器拒绝，如果想启用该功能，请在地址栏内输入 about:config,然后将项 signed.applets.codebase_principal_support 值该为true");
			}
		}
		var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
		prefs.setCharPref('browser.startup.homepage', window.location.href);
	} else {
		alert('您的浏览器不支持自动自动设置首页, 请使用浏览器菜单手动设置!');
	}
}

function addFavorite() {
	if ( document.all ) {
		try {
			window.external.addFavorite(window.location.href, document.title);
		} catch (e) {
			alert("加入收藏失败，请使用Ctrl+D进行添加");
		}

	} else if ( window.sidebar ) {
		window.sidebar.addPanel(document.title, window.location.href, "");
	} else {
		alert("加入收藏失败，请使用Ctrl+D进行添加");
	}
}

function ReImgSize(ele, size){
	$('#'+ele).find('img').each(function(i,e){
		if ($(e).width()>size) {
			$(e).height((size/$(e).width())*$(e).height());
			$(e).width(size);
		}
	})
}